package com.baizhi.service.impl;

import com.baizhi.dao.AddressDao;
import com.baizhi.Address;
import com.baizhi.service.AddressService;
import com.baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class AddressServiceImpl implements AddressService {
    @Override
    public List<Address> findAddress(int uid) {
        SqlSession session = MyBatisUtils.openSession();
        List<Address> address=new ArrayList<>();
        try {
            AddressDao mapper = session.getMapper(AddressDao.class);
            address = mapper.findAddress(uid);
        } catch (Exception e) {
            throw  new RuntimeException(e);
        } finally {
            session.close();
        }
        return address;
    }

    @Override
    public boolean insertAddress(String name, String address, String phone, int uid) {
        SqlSession session = MyBatisUtils.openSession();
        try {
            AddressDao dao = session.getMapper(AddressDao.class);
            dao.Address(name,address,phone,uid);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            throw  new RuntimeException(e);
        } finally {
            session.close();
        }
        return true;
    }

    @Override
    public boolean updateState(String column, int id,int state) {
        SqlSession session = MyBatisUtils.openSession();
        try {
            AddressDao dao = session.getMapper(AddressDao.class);
            dao.updateState(column,id,state);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            throw  new RuntimeException(e);
        } finally {
            session.close();
        }
        return true;
    }

    @Override
    public boolean updateState() {
        SqlSession session = MyBatisUtils.openSession();
        try {
            AddressDao dao = session.getMapper(AddressDao.class);
            dao.updateState1();
            session.commit();
        } catch (Exception e) {
            session.rollback();
            throw  new RuntimeException(e);
        } finally {
            session.close();
        }
        return true;
    }
}
